using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Campus.Entities;
using CampusSite.Models;
using CampusSite.Security;

namespace CampusSite.Controllers
{
    [RolActionFilter]
    [RoleAuthorize(Constantes.RolAdministrador)]
    public class ObrasSocialesController : Controller
    {
		private readonly IObraSocialRepository obrasocialRepository;
        private readonly IEstudianteRepository estudianteRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public ObrasSocialesController() : this(new ObraSocialRepository(), new EstudianteRepository())
        {
        }

        public ObrasSocialesController(IObraSocialRepository obrasocialRepository, EstudianteRepository estudianteRepository)
        {
			this.obrasocialRepository = obrasocialRepository;
            this.estudianteRepository = estudianteRepository;
        }

        //
        // GET: /ObrasSociales/

        public ViewResult Index()
        {
            return View(obrasocialRepository.All);
        }

        //
        // GET: /ObrasSociales/Details/5

        public ViewResult Details(int id)
        {
            return View(obrasocialRepository.Find(id));
        }

        //
        // GET: /ObrasSociales/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ObrasSociales/Create

        [HttpPost]
        public ActionResult Create(ObraSocial obrasocial)
        {
            if (ModelState.IsValid) {
                obrasocialRepository.InsertOrUpdate(obrasocial);
                obrasocialRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }
        
        //
        // GET: /ObrasSociales/Edit/5
 
        public ActionResult Edit(int id)
        {
             return View(obrasocialRepository.Find(id));
        }

        //
        // POST: /ObrasSociales/Edit/5

        [HttpPost]
        public ActionResult Edit(ObraSocial obrasocial)
        {
            if (ModelState.IsValid) {
                obrasocialRepository.InsertOrUpdate(obrasocial);
                obrasocialRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }

        //
        // GET: /ObrasSociales/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(obrasocialRepository.Find(id));
        }

        //
        // POST: /ObrasSociales/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var estudiantes = estudianteRepository.All.Where(e=> e.ObraSocial != null && e.ObraSocialId == id).ToList();

            if (estudiantes.Count > 0)
            {
                ModelState.AddModelError("NoPuedeEliminarLaObraSocial", "No podra eliminar la obra social porque existen estudiantes asociados");
                return View(obrasocialRepository.Find(id));
            }
            else
            {

                obrasocialRepository.Delete(id);
                obrasocialRepository.Save();
            }
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                obrasocialRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

